package com.webserver.controller;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.core.ClientHandler;
import com.webserver.entity.Article;
import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ArticleController {
    private static File rootDir,staticDir;
    private static File articleDir;

    static {
        try {
            rootDir = new File(ClientHandler.class.getClassLoader().getResource(".").toURI());
            staticDir = new File(rootDir,"static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        articleDir = new File("articles");
        if(!articleDir.exists()){
            articleDir.mkdirs();
        }
    }

    @RequestMapping("/myweb/showAllArticle")
    public void showAllArticle(HttpServletRequest request, HttpServletResponse response){
        List<Article> articleList = new ArrayList<>();
        File[] files = articleDir.listFiles(f->f.getName().endsWith(".obj"));
        for(File f : files){
            try(
                    FileInputStream fis = new FileInputStream(f);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                Article article = (Article)ois.readObject();
                articleList.add(article);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        System.out.println("文章列表"+articleList);

        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>文章列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>文章列表</h1>");
            pw.println("<table border=\"1\" >");
            pw.println("<tbody align = \"center\">");
            pw.println("<tr>");
            pw.println("<td>文章题目</td>");
            pw.println("<td>作者</td>");
            pw.println("<td>文章内容</td>");
            pw.println("</tr>");
            for(Article article : articleList){
                pw.println("<tr>");
                pw.println("<td>" + article.getTitle() + "</td>");
                pw.println("<td>" + article.getAuthor() + "</td>");
                try {
                    pw.println("<td>" + URLDecoder.decode(article.getContent(),"UTF-8") + "</td>");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                pw.println("</tr>");
            }
            pw.println("</tbody>");
            pw.println("</table>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
        System.out.println("页面已生成");

    }

    @RequestMapping("/myweb/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response){
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");

        if(title == null || author == null || content == null){
//            File file = new File(staticDir,"/myweb/article_info_error.html");
//            response.setContentFile(file);
            response.sendRedirect("/myweb/article_info_error.html");
            return;
        }

        File articleFile = new File(articleDir,title + ".obj");
        if(articleFile.exists()){
            response.sendRedirect("/myweb/have_article.html");
            return;
        }
        Article article = new Article(title,author,content);
        try (
                FileOutputStream fos = new FileOutputStream(articleFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ){
            oos.writeObject(article);
            response.sendRedirect("/myweb/article_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
